package com.pkuse.bjousystem.controller;

import com.pkuse.bjousystem.dto.school.GraduationRegistryStudentsDetailDTO;
import com.pkuse.bjousystem.dto.school.enrollment.EnrollmentDTO;
import com.pkuse.bjousystem.dto.school.enrollment.EnrollmentDetailDTO;
import com.pkuse.bjousystem.dto.school.graduation.GraduationDTO;
import com.pkuse.bjousystem.dto.school.registry.RegistryDTO;
import com.pkuse.bjousystem.dto.student.XueShengBiYeNianDuXueQiDTO;
import com.pkuse.bjousystem.dto.student.XueShengLeiBieDTO;
import com.pkuse.bjousystem.dto.student.XueShengRuXueNianDuXueQiDTO;
import com.pkuse.bjousystem.dto.student.XueShengZhuCeNianDuXueQiDTO;
import com.pkuse.bjousystem.dto.zhuanye.ZhuanYeDaiMaDTO;
import com.pkuse.bjousystem.repository.school.SchoolIDOption;
import com.pkuse.bjousystem.repository.school.SchoolRepository;
import com.pkuse.bjousystem.repository.school.SchoolTableOptions;
import com.pkuse.bjousystem.repository.school.ZhuanYeDaiMaRepository;
import com.pkuse.bjousystem.repository.student.StudentRepository;
import com.pkuse.bjousystem.repository.student.XueShengBiYeRepository;
import com.pkuse.bjousystem.repository.student.XueShengLeiBieDaiMaRepository;
import com.pkuse.bjousystem.repository.student.XueShengXuanKeJieGuoRepository;
import com.pkuse.bjousystem.utils.PageUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/school/api/v1")
public class SchoolController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    SchoolRepository schoolRepository;

    @Autowired
    ZhuanYeDaiMaRepository zhuanYeDaiMaRepository;

    @Autowired
    XueShengLeiBieDaiMaRepository xueShengLeiBieDaiMaRepository;

    @Autowired
    XueShengBiYeRepository xueShengBiYeRepository;

    @Autowired
    XueShengXuanKeJieGuoRepository xueShengXuanKeJieGuoRepository;

    @Autowired
    StudentRepository studentRepository;

    @GetMapping("/check")
    public String check() {
        return "ok";
    }

    // graduation

    @PostMapping("/graduation/ndxq")
    public List<XueShengBiYeNianDuXueQiDTO> getGraduationNdxqOptions(@RequestBody Map<String, String> body) {
        SchoolIDOption schoolIDOption = new SchoolIDOption(body);
        return xueShengBiYeRepository.listBiYeXueQiForGraduation(schoolIDOption.getId().orElse(null));
    }

    @PostMapping("/graduation/xslb")
    public List<XueShengLeiBieDTO> getGraduationXslbOptions(@RequestBody Map<String, String> body) {
        SchoolIDOption schoolIDOption = new SchoolIDOption(body);
        return xueShengLeiBieDaiMaRepository.listXueShengLeiBieForGraduation(schoolIDOption.getId().orElse(null));
    }

    @PostMapping("/graduation/zylb")
    public List<ZhuanYeDaiMaDTO> getGraduationZylbOptions(@RequestBody Map<String, String> body) {
        SchoolIDOption schoolIDOption = new SchoolIDOption(body);
        return zhuanYeDaiMaRepository.listZhuanYeDaiMaForGraduation(schoolIDOption.getId().orElse(null));
    }

    @PostMapping("/graduation")
    public Page<GraduationDTO> listGraduation(@RequestBody Map<String, String> body) {
        SchoolTableOptions options = new SchoolTableOptions(body);
        logger.debug("options:{}", options);
        Pageable pageable = PageUtils.fromRequest(body);
        logger.debug("page:{}", pageable);
        return schoolRepository.listGraduation(options, pageable);
    }

    @PostMapping("/graduation/details")
    public Page<GraduationRegistryStudentsDetailDTO> listGraduationDetails(@RequestBody Map<String, String> body) {
        return schoolRepository.listGraduationDetails(body.get("xxdm"), body.get("bynd"), body.get("byxq"), body.get("xslbdm"), body.get("zydm"), PageUtils.fromRequest(body));
    }

    // registry

    @PostMapping("/registry/ndxq")
    public List<XueShengZhuCeNianDuXueQiDTO> getRegistryNdxqOptions(@RequestBody Map<String, String> body) {
        SchoolIDOption schoolIDOption = new SchoolIDOption(body);
        return xueShengXuanKeJieGuoRepository.listZhuCeXueQiForRegistry(schoolIDOption.getId().orElse(null));
    }

    @PostMapping("/registry/xslb")
    public List<XueShengLeiBieDTO> getRegistryXslbOptions(@RequestBody Map<String, String> body) {
        SchoolIDOption schoolIDOption = new SchoolIDOption(body);
        return xueShengLeiBieDaiMaRepository.listXueShengLeiBieForRegistry(schoolIDOption.getId().orElse(null));
    }

    @PostMapping("/registry/zylb")
    public List<ZhuanYeDaiMaDTO> getRegistryZylbOptions(@RequestBody Map<String, String> body) {
        SchoolIDOption schoolIDOption = new SchoolIDOption(body);
        return zhuanYeDaiMaRepository.listZhuanYeDaiMaForRegistry(schoolIDOption.getId().orElse(null));
    }

    @PostMapping("/registry")
    public Page<RegistryDTO> listRegistry(@RequestBody Map<String, String> body) {
        SchoolTableOptions options = new SchoolTableOptions(body);
        logger.debug("options:{}", options);
        Pageable pageable = PageUtils.fromRequest(body);
        logger.debug("page:{}", pageable);
        return schoolRepository.listRegistry(options, pageable);
    }

    @PostMapping("/registry/details")
    public Page<GraduationRegistryStudentsDetailDTO> listRegistryDetails(@RequestBody Map<String, String> body) {
        return schoolRepository.listRegistryDetails(body.get("xxdm"), body.get("zcnd"), body.get("zcxq"), body.get("xslbdm"), body.get("zydm"), PageUtils.fromRequest(body));
    }

    // enrollment

    @PostMapping("/enrollment/ndxq")
    public List<XueShengRuXueNianDuXueQiDTO> getEnrollmentNdxqOptions(@RequestBody Map<String, String> body) {
        SchoolIDOption schoolIDOption = new SchoolIDOption(body);
        return studentRepository.listRuXueXueQiForEnrollment(schoolIDOption.getId().orElse(null));
    }

    @PostMapping("/enrollment/xslb")
    public List<XueShengLeiBieDTO> getEnrollmentXslbOptions(@RequestBody Map<String, String> body) {
        SchoolIDOption schoolIDOption = new SchoolIDOption(body);
        return xueShengLeiBieDaiMaRepository.listXueShengLeiBieForEnrollment(schoolIDOption.getId().orElse(null));
    }

    @PostMapping("/enrollment/zylb")
    public List<ZhuanYeDaiMaDTO> getEnrollmentZylbOptions(@RequestBody Map<String, String> body) {
        SchoolIDOption schoolIDOption = new SchoolIDOption(body);
        return zhuanYeDaiMaRepository.listZhuanYeDaiMaForEnrollment(schoolIDOption.getId().orElse(null));
    }

    @PostMapping("/enrollment")
    public Page<EnrollmentDTO> listEnrollment(@RequestBody Map<String, String> body) {
        SchoolTableOptions options = new SchoolTableOptions(body);
        logger.debug("options:{}", options);
        Pageable pageable = PageUtils.fromRequest(body);
        logger.debug("page:{}", pageable);
        return schoolRepository.listEnrollment(options, pageable);
    }

    @PostMapping("/enrollment/details")
    public Page<EnrollmentDetailDTO> listEnrollmentDetails(@RequestBody Map<String, String> body) {
        return schoolRepository.listEnrollmentDetails(body.get("xxdm"), body.get("rxnd"), body.get("rxxq"), body.get("xslbdm"), body.get("zydm"), PageUtils.fromRequest(body));
    }
}
